Introduction
This artilce explains how to test HTML Controls which are on ASPX page through NUnitASP. Usually NUnitASP tests the web controls on ASPX page. Even though NUnit has provided enough support to test most of the web controls some times it needs to test HTML controls of ASPX page. NUnit has provided 2 tester objects for HTML controls. In this artilce I just brief thruogh how to test the other HTML controls like Label, Button, Table etc..
NUnitASP uses Response text to test the values of Web Controls or HTML Controls. Response text is usually in HTML format. That means IIS emits HTML format for the web controls in the response. We can take this Response text as advantage and search thruogh the response text and find the value for the required HTML controls.
Testing the Web Controls
Testing web controls with NUnitASP is a straight farword method. In the first step, declare the Tester object for the control which you want to test.
lblHeader = new LabelTester("lblHeading", CurrentWebForm);
lblRep = new LabelTester("lblRepName", CurrentWebForm);
txtName = new TextBoxTester("txtUserName",CurrentWebForm);
btnReport = new ButtonTester("btnRun", CurrentWebForm);
grdData = new DataGridTester("dgData",CurrentWebForm);
Next, Call GetPage method of browser, which sends a request to the page.
Browser.GetPage("http://localhost/GetTrackerReportTest/GetReport.aspx">http:);
Now, test the Controls for the required property
AssertVisibility(lblHeader,true);
AssertVisibility(lblRep,true);
AssertVisibility(txtName,true);
AssertVisibility(btnReport,true);
AssertVisibility(grdData,false);
Find more details on how to test Web Conrols at
http://nunitasp.sourceforge.net/tutorial/index.html
Testing the HTML Controls
Testing process is same for even HTML Controls. Only the thing is we need to find equivalent web control for HTML Control and create object to Web Control tester class with HTML control ID. For example if you want to test HTML input control, create an object to TextBox web control with HTML control id.
Ex: If the following is HTML Control that needs to be tested,
<input type="text" id="HTMLTextBox" value="HTML Textbox Value"
Then you need to create object for TextBoxTester
TextBoxTester txtHTMLName = new TextBoxTester("HTMLTextBox",CurrentWebForm);
Once, object is created, you can test HTML Control as you are testing the web control.
Simillarly for HTML Label create and object of LabelTester, for HTML Table create and object of DataGridTester.
LabelTester lblHTML = new LabelTester("HTMLLabel", CurrentWebForm);
DataGridTester HTMLDGTester = new DataGridTester("HTMLTable",CurrentWebForm);
Remember that, when you are testing HTML Table with DataGridTester object, row index will start from -1 not from 0. Usually, In datagrid case -1 row returns Header row. All the HTML Control must contain ID to test it.
string[] strRow0 = {"11","12","13"};
string[] strRow1 = {"21","22","23"};
string[] strRow2 = {"31","32","33"};
DataGridTester HTMLDGTester = new DataGridTester("HTMLTable",CurrentWebForm);
AssertEquals("HTML Table Failed at Row1", strRow0, HTMLDGTester.GetRow(-1).TrimmedCells);
AssertEquals("HTML Table Failed at Row2", strRow1, HTMLDGTester.GetRow(0).TrimmedCells);
AssertEquals("HTML Table Failed at Row3", strRow2, HTMLDGTester.GetRow (1).TrimmedCells);
Conclusion
For testing the HTML Controls with NUnitASP, you can also use regullar expressions to find the control id. Once the control id is found in response text, parse the statement for the required value.